import {
  Message
} from 'element-ui';
import Base from 'models/base';
import moment from 'moment';
import SystemUtil from 'utils/systemUtil';

export default class GoodsForm extends Base {
  constructor(goods, user, type) {
    super();

    if (goods === undefined) {
      this.initData(user);
    } else {
      this.setData(goods, type);
    }
  }

  initData(user) {
    let fields = ['goodsName', 'goodsDescription', 'goodsTips', 'buyMethod'];
    this.resetObject(fields, '');

    this.goodsPic = [];
    this.goodsModelList = [];
    this.majorType = '';
    this.majorTypeName = '';
    this.status = -2;
    this.activityTypeId = '';
    this.activityStartTime = null;
    this.activityEndTime = null;
    this.activityRemark = '';

    let userFields = ['branchId', 'branchName', 'partnerId', 'partnerName'];
    this.initObject(user, userFields);
  }

  setData(goods, type) {
    let fields = ['goodsName', 'goodsDescription', 'goodsTips', 'buyMethod', 'status', 'branchId', 'branchName', 'partnerId', 'partnerName'];
    this.initObject(goods, fields);

    let goodsPic = goods.goodsPic;
    if (SystemUtil.isEmpty(goodsPic)) {
      goodsPic = [];
    } else {
      goodsPic = goodsPic.split(',');
    }
    this.goodsPic = goodsPic;

    this.goodsModelList = [];
    this.id = goods.goodsDefId;

    let majorType = goods.goodsMajorType;
    if (SystemUtil.isEmpty(majorType)) {
      majorType = '';
    } else {
      majorType = JSON.parse(majorType);
    }
    let majorTypeName = goods.goodsMajorTypeName;
    if (SystemUtil.isEmpty(majorTypeName)) {
      majorTypeName = [];
    } else {
      majorTypeName = JSON.parse(majorTypeName);
    }
    this.majorType = majorType;
    this.majorTypeName = majorTypeName;

    let applyActivityTypeId = goods.applyActivityTypeId;
    if (applyActivityTypeId === null) {
      applyActivityTypeId = '';
      this.activityStartTime = null;
      this.activityEndTime = null;
      this.activityRemark = '';
      this.activityTypeName = '无';
    } else {
      this.activityStartTime = moment(goods.applyActivityStartTime).valueOf();
      this.activityEndTime = moment(goods.applyActivityEndTime).valueOf();
      this.activityRemark = goods.applyActivityRemark;
      this.activityTypeName = goods.activityTypeName;
    }
    this.activityTypeId = applyActivityTypeId;

    let status = goods.status;
    if (type === 'table') {
      this.hasUpdate = [-1, 1].indexOf(status) === -1;
      this.hasLook = [-1, 1].indexOf(status) !== -1;
      this.hasApply = status === -2;
      this.hasUpper = status === 2;
      this.hasLower = status === 1;
    } else if (type === 'approval') {
      this.hasApproval = status === -1;
      this.hasRetreat = status !== 0;
    }
  }

  addGoodsModel(goodsModelForm) {
    let goodsModelList = this.goodsModelList;
    let obj = _.find(goodsModelList, {
      goodsModelName: goodsModelForm.goodsModelName
    });
    if (obj !== undefined) {
      Message('规格名称重复');
      return false;
    }
    goodsModelList.push(goodsModelForm);
    return true;
  }

  updateGoodsModel(goodsModelForm, oldGoodsModel) {
    let goodsModelList = this.goodsModelList;
    let index = -1;
    for (let i = 0; i < goodsModelList.length; i++) {
      const obj = goodsModelList[i];
      if (obj === oldGoodsModel) {
        index = i;
        continue;
      }
      if (obj.goodsModelName === goodsModelForm.goodsModelName) {
        Message('规格名称重复');
        return false;
      }
    }

    goodsModelList.splice(index, 1, goodsModelForm);
    return true;
  }

  delGoodsModel(goodsModelForm) {
    let goodsModelList = this.goodsModelList;
    let index = goodsModelList.indexOf(goodsModelForm);
    goodsModelList.splice(index, 1);
  }

  resetGoodModelList(goodsModelList) {
    this.goodsModelList = goodsModelList;
  }

  checkValid(hasCreateNew = false) {
    let fields = ['goodsName', 'buyMethod', 'majorType'];
    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let activityTypeId = this.activityTypeId;
    if (activityTypeId !== '') {
      code = this.checkRequired(['activityStartTime', 'activityEndTime', 'activityRemark']);
      if (code !== '') {
        return code;
      }

      let startTime = this.activityStartTime;
      let endTime = this.activityEndTime;
      if (moment(startTime).isAfter(endTime, 'second')) {
        return 'startTime>endTime';
      }

      let activityRemark = this.activityRemark;
      if (activityRemark.trim().length < 10) {
        return 'activityRemark<10';
      }
    }

    code = this.checkRequired(['goodsDescription', 'goodsTips', 'goodsPic', 'goodsModelList']);
    if (code !== '') {
      return code;
    }

    if (!hasCreateNew && activityTypeId !== '') {
      let goodsModelList = this.goodsModelList;
      if (goodsModelList.length > 1) {
        return 'goodsModelList>1';
      }
    }

    return '';
  }

  getObject(hasCreateNew = false,hasAdd = false) {
    let postData = {
      status: -2
    };
    let fields = ['goodsName', 'branchId', 'partnerId', 'buyMethod', 'goodsDescription', 'goodsTips'];
    this.setPostData(postData, fields, true);

    let id = this.id;
    if (!hasCreateNew && id !== undefined) {
      postData.goodsDefId = id;
    }

    let majorType = this.majorType;
    postData.goodsMajorType = JSON.stringify(majorType);

    let goodsPic = this.goodsPic;
    postData.goodsPic = goodsPic.join(',');

    if (!hasCreateNew) {
      let activityTypeId = this.activityTypeId;
      if (activityTypeId !== '') {
        postData.applyActivityTypeId = activityTypeId;
        let startTime = this.activityStartTime;
        postData.applyActivityStartTime = moment(startTime).format('YYYY-MM-DD HH:mm:ss');
        let endTime = this.activityEndTime;
        postData.applyActivityEndTime = moment(endTime).format('YYYY-MM-DD HH:mm:ss');
        postData.applyActivityRemark = this.activityRemark;
      }
    }

    if (hasAdd) {
      let goodsModelList = this.goodsModelList;
      goodsModelList = _.map(goodsModelList,goodsModel=>{
        let data = goodsModel.getObject(null);
        delete data.goodsDefId;
        return data;
      });
      postData.goodsModels = goodsModelList;
    }

    return postData;
  }

  getModelList(goodsDefId) {
    let goodsModelList = this.goodsModelList;
    let goodsModels = [];
    for (let i = 0; i < goodsModelList.length; i++) {
      const goodsModel = goodsModelList[i];
      let postData = goodsModel.getObject(goodsDefId);
      goodsModels.push(postData);
    }
    return goodsModels;
  }
}
